package com.example.resolve.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName:RabbitConfig
 * Package:com.example.problem01.config
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/8/13-20:47
 * @Version:v1.0
 */
@Configuration
public class RabbitConfig {


    @Value("${my.exchangeName}")
    private String exchangeName;

    @Value("${my.queueOrderName}")
    private String queueOrderName;

    @Value("${my.queuePayName}")
    private String queuePayName;

    @Value("${my.queueDlxName}")
    private String queueDlxName;



    @Bean
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange(exchangeName).build();
    }

    @Bean
    public Queue queueOrderNormal() {

        return QueueBuilder
                .durable(queueOrderName)
                .deadLetterExchange(exchangeName)//死信交换机
                .deadLetterRoutingKey("error")//死信路由key
                .build();

    }

    @Bean
    public Queue queuePayNormal() {

        return QueueBuilder
                .durable(queuePayName)
                .deadLetterExchange(exchangeName)//死信交换机
                .deadLetterRoutingKey("error")//死信路由key
                .build();

    }

    @Bean
    public Queue dlxQueue(){
        return QueueBuilder.durable(queueDlxName).build();
    }



    @Bean
    public Binding binding(DirectExchange directExchange, Queue queueOrderNormal) {
        return BindingBuilder.bind(queueOrderNormal).to(directExchange).with("order");
    }

    @Bean
    public Binding bindingPay(DirectExchange directExchange ,Queue queuePayNormal){
        return BindingBuilder.bind(queuePayNormal).to(directExchange).with("pay");
    }

    @Bean
    public Binding bindingDlx(DirectExchange directExchange,Queue dlxQueue){
        return BindingBuilder.bind(dlxQueue).to(directExchange).with("error");
    }

}
